using System;
using BitBenderGames;
using UnityEngine;

namespace Yoozoo.Gameplay.City
{
    public class CityTouchManager : MonoBehaviour
    {
        private MobileTouchCamera _mobileTouchCamera;
        private TouchInputController _touchInputController;
        
        public RaycastHit[] clickHits = new RaycastHit[10];
        public Action<int, RaycastHit[],bool> onClick;
        public Action<int, RaycastHit[],bool,bool,bool> onClickEx;

        public Action onTouchUp;

        public int touchCount = 0;
        public RaycastHit[] touchHits = new RaycastHit[10];
        public Action<int, RaycastHit[],bool> onTouchStart;
        public Action<float, int, RaycastHit[],bool> onTouchDown;

        public Action<Vector3,bool,bool,bool> onClickScreen;

        public Action<Vector3, bool, bool> onDragStart;

        public Action<Vector3, float> onPinchStart;


        public void Init(MobileTouchCamera mobileTouchCamera)
        {
            if (mobileTouchCamera == null)
            {
                return;
            }
            
            _mobileTouchCamera = mobileTouchCamera;
            _touchInputController = mobileTouchCamera.TouchInputController;
            
            Register();
        }
        
        public void OnDispose()
        {
            UnRegister();
        }

        public void Register()
        {
            // 初始化事件
            _touchInputController.OnInputClick += OnTouchClick;
            _touchInputController.OnFingerUp += OnTouchUp;
            _touchInputController.OnFingerDown += OnTouchStart;
            _touchInputController.OnTouchDown += OnTouchDown;
            _touchInputController.OnDragStart += OnDragStart;
            _touchInputController.OnDragUpdate += OnDrag;
            _touchInputController.OnPinchStart += OnPinchStart;
        }

        public void UnRegister()
        {
            _touchInputController.OnInputClick -= OnTouchClick;
            _touchInputController.OnFingerUp -= OnTouchUp;
            _touchInputController.OnFingerDown -= OnTouchStart;
            _touchInputController.OnTouchDown -= OnTouchDown;
            _touchInputController.OnDragStart -= OnDragStart;
            _touchInputController.OnDragUpdate -= OnDrag;
            _touchInputController.OnPinchStart -= OnPinchStart;
        }
        
        
        private void OnTouchClick(Vector3 clickposition, bool isdoubleclick, bool islongtap, bool isonui)
        {
            onClickScreen?.Invoke(clickposition,isdoubleclick,islongtap,isonui);
            
            int count = GetRayCast(clickposition, clickHits);
            if (count > 0)
            {
                onClick?.Invoke(count,clickHits,isonui);
                onClickEx?.Invoke(count,clickHits,isdoubleclick,islongtap,isonui);
            }
        }
        
        private void OnTouchUp()
        {

            touchCount = 0;
        }

        private void OnTouchStart(Vector3 pos, bool isonui)
        {
            touchCount = GetRayCast(pos, touchHits);
            if (touchCount > 0)
            {
                onTouchStart?.Invoke(touchCount,touchHits,isonui);
            }
        }

        private void OnTouchDown(float holdtime, bool isonui)
        {
            if (touchCount > 0)
            {
                onTouchDown?.Invoke(holdtime,touchCount,touchHits,isonui);
            }
        }

        private void OnDragStart(Vector3 pos, bool islongtap, bool isonui)
        {
            onDragStart?.Invoke(pos,islongtap,isonui);
        }

        private void OnDrag(Vector3 dragposstart, Vector3 dragposcurrent, Vector3 correctionoffset, bool isOnUI)
        {
            
        }

        private void OnPinchStart(Vector3 pinchCenter, float pinchDistance)
		{
            onPinchStart?.Invoke(pinchCenter, pinchDistance);
        }


        private int GetRayCast(Vector3 pos,RaycastHit[] hits)
        {
            Ray camRay = _mobileTouchCamera.Cam.ScreenPointToRay(pos);
            int count = Physics.RaycastNonAlloc(camRay, hits,1000);
            return count;
        }

        //private void LateUpdate()
        //{

        //}


        
    }
}